package org.new9uy.tetris.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import org.new9uy.tetris.GameConstants;
import org.new9uy.tetris.game.player.Player;
import org.new9uy.tetris.game.type.Game;
import org.new9uy.tetris.model.Matrix;
import org.new9uy.tetris.model.Next;

@SuppressWarnings("serial")
public class PlayerPanel extends JPanel
{
    private JLabel scoreLabel;
    private JLabel levelLabel;
    private JLabel linesLabel;
    private JLabel nameLabel;
    
    private JPanel nextPanel;
    private JPanel gamePanel;
    private JPanel dataPanel;
    
    private Player player;

    public PlayerPanel(Player player)
    {
        super(new BorderLayout(5, 5));
        this.player = player;
        
        init();
    }
    
    private void init()
    {
        Matrix mtx = Game.matrix;
        Next next = Game.next;

        gamePanel = new JPanel(new GridLayout(20, 10));
        
        nameLabel = new JLabel(player.getName());
        
        if (player.isBot())
        {
            Font f = nameLabel.getFont();
            Font botFont = new Font(f.getName(), Font.ITALIC, f.getSize());
            nameLabel.setFont(botFont);
        }
        
        nameLabel.setHorizontalAlignment(JLabel.LEFT);

        for (int j = 20; j > 0; j--)
        {
            for (int i = 1; i < 11; i++)
            {
                gamePanel.add(mtx.getMino(i, j).getLabel());
            }
        }

        JPanel nextPanel0 = new JPanel(new GridLayout(4, 4));
        for (int t = 0; t < 4; t++)
        {
            for (int p = 0; p < 4; p++)
            {
                nextPanel0.add(next.getMino(t, p).getLabel());
            }
        }
        nextPanel = new JPanel(new FlowLayout());

        nextPanel.add(nextPanel0);

        scoreLabel = new JLabel();
        levelLabel = new JLabel();
        linesLabel = new JLabel();
        
        refreshData();
        
        scoreLabel.setPreferredSize(GameConstants.VIEW_DATA_DIM);
        levelLabel.setPreferredSize(GameConstants.VIEW_DATA_DIM);

        dataPanel = new JPanel(new GridLayout(4, 1));
        
        dataPanel.add(nextPanel);
        dataPanel.add(levelLabel);
        dataPanel.add(linesLabel);
        dataPanel.add(scoreLabel);

        add(nameLabel, BorderLayout.NORTH);
        add(gamePanel, BorderLayout.CENTER);
        add(dataPanel, BorderLayout.EAST);
    }

    public final void refreshData()
    {
        scoreLabel.setText("Score: "
                + Game.score.getScore());

        levelLabel.setText("Level :"
                + Game.current().getPlayer().getGravity().getLvl());

        linesLabel.setText("Lines :"
                + Game.score.totalLinesDone());
    }

    public JLabel getScoreLabel()
    {
        return scoreLabel;
    }

    public JLabel getLevelLabel()
    {
        return levelLabel;
    }

    public JLabel getLinesLabel()
    {
        return linesLabel;
    }

    public JLabel getNameLabel()
    {
        return nameLabel;
    }
}
